package zerous.mytest;

import android.content.*;
import android.os.*;
import android.support.design.widget.*;
import android.support.v4.app.*;
import android.support.v4.widget.*;
import android.support.v7.app.*;
import android.support.v7.widget.*;
import android.view.*;
import android.widget.*;

import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.util.*;
import android.view.View.*;
import android.content.res.*;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
	enum Fragments {
		None, CharView, Main, Password;
	}

	CharViewFragment cvFrag;
	MainFragment mFrag;
	PasswordFragment pFrag;

	Fragments state;
	FragmentTransaction ft;
	FragmentManager mgr;
	Toolbar toolbar;

	MenuInflater inflater;
	DrawerLayout dl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

		toolbar = (Toolbar)findViewById(R.id.mainToolbar);
		setSupportActionBar(toolbar);
		getSupportActionBar().setHomeButtonEnabled(true);

		mgr = getSupportFragmentManager();

		dl = (DrawerLayout)findViewById(R.id.mainDrawerLayout);

		ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, dl, toolbar, R.string.app_name, R.string.app_name);
		drawerToggle.syncState();
		dl.setDrawerListener(drawerToggle);

		NavigationView navi = (NavigationView)findViewById(R.id.mainNavigation);
		navi.getHeaderView(0).setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					ft = mgr.beginTransaction();
					ft.replace(R.id.mainFrameLayout, mFrag);
					ft.commit();
					setTitle(R.string.app_name);
					state = Fragments.Main;
					dl.closeDrawer(Gravity.LEFT);
				}
			}
		);
		navi.setNavigationItemSelectedListener(this);

		cvFrag = new CharViewFragment();
		mFrag = new MainFragment();
		pFrag = new PasswordFragment();
		if (getIntent().getStringExtra("fragments") != null)
			switch (getIntent().getStringExtra("fragments")) {
				case "charViewer":
					ft = mgr.beginTransaction();
					ft.replace(R.id.mainFrameLayout, cvFrag);
					ft.commit();
					break;

				case "password":
					ft = mgr.beginTransaction();
					ft.replace(R.id.mainFrameLayout, pFrag);
					ft.commit();
					break;

				case "main":	
				default:
					ft = mgr.beginTransaction();
					ft.replace(R.id.mainFrameLayout, mFrag);
					ft.commit();
			}
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		inflater = new MenuInflater(this);
		inflater.inflate(R.menu.main_o, menu);
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {

		switch (state) {
			case CharView:
				cvFrag.setMenu(menu, inflater);
				break;

			default:
				menu.clear();
				inflater.inflate(R.menu.main_o, menu);
		}
		return true;
	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (state == Fragments.None) {
			switch (item.getItemId()) {
				case android.R.id.home:
					Toast.makeText(this, "Home", Toast.LENGTH_SHORT).show();
					break;

				default:
					return false;
			}
		} else if (state == Fragments.CharView) {
			cvFrag.menuSelected(item);
		}
		return true;
	}

	@Override
	public boolean onNavigationItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.charViewer:
				ft = mgr.beginTransaction();
				ft.replace(R.id.mainFrameLayout, cvFrag);
				ft.commit();
				setTitle(R.string.char_view);
				state = Fragments.CharView;
				break;

			case R.id.password:
				ft = mgr.beginTransaction();
				ft.replace(R.id.mainFrameLayout, pFrag);
				ft.commit();
				setTitle(R.string.password_creator);
				state = Fragments.Password;
				break;

			case R.id.editor:
				Intent i = new Intent(this, EditorActivity.class);
				startActivity(i);
				finish();
				break;

			default:
				return false;
		}
		dl.closeDrawer(Gravity.LEFT);
		return true;
	}
}
